package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.*;
import com.itheima.health.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @ClassName MenuServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/10/13 9:54
 * @Version V1.0
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Map<String, Object>> getMenu(String username) {
        //用于存储菜单
        List<Map<String, Object>> mapList = new ArrayList<>();


        //先根据用户名查询到用户
        User user = userDao.findUserByUsername(username);

        //根据用户查询角色
        Set<Role> roles = roleDao.findRolesByUserId(user.getId());

        for (Role role : roles) {
            for (Menu menu : role.getMenus()) {
                Map<String, Object> map = new HashMap<>();
                map.put("path",menu.getPath());
                map.put("title",menu.getName());
                map.put("icon",menu.getIcon());
                //查询子菜单
                List<Menu> childrens = menuDao.findChildrenByParentMenuId(menu.getId());
                List<Map<String,Object>> childrenMapList = new ArrayList<>();
                for (Menu children : childrens) {

                    Map<String, Object> childrenMap = new HashMap<>();
                    childrenMap.put("path",children.getPath());
                    childrenMap.put("title",children.getName());
                    childrenMap.put("linkUrl",children.getLinkUrl());
                    childrenMapList.add(childrenMap);
                }
                map.put("children",childrenMapList);

                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());


        Page<Menu> page = menuDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Menu findById(Integer id) {
                return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public void delete(Integer id) {

        Integer count=menuDao.findRoleCountOfTheMenuId(id);
        if(count>0){
            throw new RuntimeException("中间表和检查组中存在关联数据，不能删除");
        }
        menuDao.delete(id);
    }


}
